package com.csweimei.rentmanagementtreasure.tenant.viewModel;

import android.app.Application;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableArrayList;
import androidx.databinding.ObservableField;
import androidx.databinding.ObservableList;

import com.chad.library.BR;
import com.csweimei.rentmanagementtreasure.R;
import com.csweimei.rentmanagementtreasure.base.AppConfig;
import com.csweimei.rentmanagementtreasure.http.entity.BaseResult;
import com.csweimei.rentmanagementtreasure.http.entity.HouseFollowRecordBean;
import com.csweimei.rentmanagementtreasure.http.entity.HouseHistoryBean;
import com.csweimei.rentmanagementtreasure.http.entity.HouseInfoDetailBean;
import com.csweimei.rentmanagementtreasure.tenant.ui.act.house.AddHouseFollowActivity;
import com.csweimei.rentmanagementtreasure.viewModel.TitleVM;

import java.util.HashMap;
import java.util.Map;

import io.reactivex.observers.DisposableObserver;
import me.goldze.mvvmhabit.base.BaseModel;
import me.goldze.mvvmhabit.utils.RxUtils;
import me.tatarka.bindingcollectionadapter2.ItemBinding;

public class HouseFRVM extends TitleVM {

    public ObservableField<Integer> sid = new ObservableField<>();
    public ObservableField<String> FollowType = new ObservableField<>("0");
    public ObservableField<String> type = new ObservableField<>("0");
    public ObservableField<HouseInfoDetailBean> houseInfo = new ObservableField<>();

    public HouseFRVM(@NonNull Application application, BaseModel model) {
        super(application, model);
        setTitleText("房源跟进记录");
        setRightIconVisible(View.VISIBLE);

    }


    @Override
    protected void rightIconOnClick() {
        super.rightIconOnClick();
        Bundle bundle = new Bundle();
        bundle.putSerializable(AppConfig.BUNDLE_KEY,houseInfo.get());
        bundle.putString("type",FollowType.get());
        startActivity(AddHouseFollowActivity.class,bundle);
    }

    public ObservableField<Integer> showF = new ObservableField<>(View.VISIBLE);
    public ObservableField<Integer> showH = new ObservableField<>(View.GONE);

    @Override
    public void onResume() {
        super.onResume();
        if (type.get().equals("0")){
            //获取房源跟进记录
            getFollowHouse();
        }else {
            //获取历史租约记录
            getHistoryLeaselist();
        }
    }

    @Override
    public void onCreate() {
        super.onCreate();
        getHouseInfo();
    }

    public ObservableList<HouseHistoryItemVM> observableListH = new ObservableArrayList<>();
    public ItemBinding<HouseHistoryItemVM> itemBindingH = ItemBinding.of(BR.viewModel,R.layout.item_house_history);

    private void getHistoryLeaselist() {
        Map<String,String> map = new HashMap<>();
        map.put("userid",model.getLoginInfo().getData().getUser_id());
        map.put("housingID",sid.get()+"");
        model.history_leaselist(map)
                .compose(RxUtils.schedulersTransformer())
                .compose(RxUtils.exceptionTransformer())
                .doOnSubscribe(this)
                .doOnSubscribe(disposable -> {})
                .subscribe(new DisposableObserver<HouseHistoryBean>() {
                    @Override
                    public void onNext(HouseHistoryBean bean) {
                        if (bean.issucc){
                            observableListH.clear();
                            showF.set(View.GONE);
                            showH.set(View.VISIBLE);
                            for (HouseHistoryBean.DataDTO dataDTO: bean.getData()) {
                                HouseHistoryItemVM houseHistoryItemVM = new HouseHistoryItemVM(HouseFRVM.this);
                                houseHistoryItemVM.setData(dataDTO);
                                observableListH.add(houseHistoryItemVM);
                            }
                        }
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    public ObservableList<HouseFRItemVM> observableListF = new ObservableArrayList<>();
    public ItemBinding<HouseFRItemVM> itemBindingF = ItemBinding.of(BR.viewModel, R.layout.item_follow_house);

    private void getFollowHouse() {
        Map<String,String> map = new HashMap<>();
        map.put("userid",model.getLoginInfo().getData().getUser_id());
        map.put("housingID",sid.get()+"");
        model.follow_housing(map)
                .compose(RxUtils.schedulersTransformer())
                .compose(RxUtils.exceptionTransformer())
                .doOnSubscribe(this)
                .doOnSubscribe(disposable -> {})
                .subscribe(new DisposableObserver<HouseFollowRecordBean>() {
                    @Override
                    public void onNext(HouseFollowRecordBean bean) {
                        if (bean.issucc){
                            observableListF.clear();
                            showF.set(View.VISIBLE);
                            showH.set(View.GONE);
                            for (HouseFollowRecordBean.DataDTO dataDTO : bean.getData()) {
                                HouseFRItemVM houseFRItemVM = new HouseFRItemVM(HouseFRVM.this);
                                houseFRItemVM.setData(dataDTO);
                                observableListF.add(houseFRItemVM);
                            }
                        }
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    private void getHouseInfo() {
        Map<String,String> map = new HashMap<>();
        map.put("userid",model.getLoginInfo().getData().getUser_id());
        map.put("sid",sid.get()+"");
        model.wholeinfo(map)
                .compose(RxUtils.schedulersTransformer())
                .compose(RxUtils.exceptionTransformer())
                .doOnSubscribe(this)
                .doOnSubscribe(disposable -> {})
                .subscribe(new DisposableObserver<HouseInfoDetailBean>() {
                    @Override
                    public void onNext(HouseInfoDetailBean bean) {
                        if (bean.issucc){
                            houseInfo.set(bean);
                        }
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
}
